#include "game.h"

Game::Game()
{
    m_currentPos = -1;
    setCurrentLevel(NULL);

    m_savedGames.readFromFile("./gameslist");

    //loadLevel("level1");
}

Game::~Game()
{
    delete m_currentLevel;
}

Level *Game::getCurrentLevel() const {return this->m_currentLevel;}
QString Game::getLevelAt(int pos) {return m_levels.at(pos);}
Map *Game::getCurrentMap() const {return m_currentLevel->getMap();}
Player *Game::getCurrentPlayer() const {return m_currentLevel->getPlayer();}

QStringList Game::getSavedGamesToString()
{
    QStringList list;

    for(int i = 0; i < m_savedGames.getCount(); i++)
        list.append(m_savedGames.getSavedGameAt(i)->toString());

    return list;
}

void Game::setCurrentLevel(Level *level) {this->m_currentLevel = level;}

void Game::nextLevel()
{
    m_currentPos++;
    if(m_currentPos >= m_levels.count()) return;

    delete m_currentLevel;
    loadLevel(m_levels.at(m_currentPos));
}

void Game::saveLevel(QString filePath)
{
    QFile file(filePath);
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);
    out << m_currentLevel;
    qDebug() << " SAVE LEVEL "  << m_currentLevel;
    file.close();
}

void Game::loadLevel(QString filePath)
{
    QFile file(filePath);
    file.open(QIODevice::ReadOnly);
    QDataStream in(&file);
    m_currentLevel = new Level();
    in >> m_currentLevel;
    qDebug() << " LOAD LEVEL "  <<  m_currentLevel;
    file.close();
}

void Game::saveGame(QString saveName, int pos)
{
    int id;

    if(pos < 0)
        id = m_savedGames.addSavedGame(saveName, m_levels, m_currentPos);
    else
        id = m_savedGames.replaceSavedGame(pos, saveName, m_levels, m_currentPos);

    m_savedGames.writeToFile("gameslist"); //A CHANGER : utiliser une variable pour le nom !!!
    saveLevel("save" + QString::number(id));
}

void Game::loadGame(int pos)
{
    SavedGame *savedGame = m_savedGames.getSavedGameAt(pos);
    m_levels = savedGame->getLevels();
    m_currentPos = savedGame->getLevelPos();

    delete m_currentLevel;
    loadLevel("save" + QString::number(savedGame->getSaveId()));
}


void Game::newGame(QStringList levels)
{
    m_levels = levels;
    m_currentPos = 0;

    delete m_currentLevel;
    loadLevel(getLevelAt(m_currentPos));
}

void Game::endGame()
{
    m_levels.clear();
    delete m_currentLevel;
    m_currentLevel = NULL;
}


void Game::removeSavedGame(int pos)
{
    m_savedGames.removeSavedGame(pos);
}


void Game::addLevel(QString filePath) {m_levels.append(filePath);}

void Game::removeLevel(int pos)
{
    m_levels.removeAt(pos);
}


void Game::collisionsWithPlayer()
{
    Player *player = getCurrentPlayer();
    m_currentLevel->playerCollidesWithMap(player);
    m_currentLevel->playerCollidesWithMonster(player);
    m_currentLevel->playerCollidesWithRock(player);
}

void Game::movePlayerLeft() {m_currentLevel->getPlayer()->moveLeft();}
void Game::movePlayerUp() {m_currentLevel->getPlayer()->moveUp();}
void Game::movePlayerRight() {m_currentLevel->getPlayer()->moveRight();}
void Game::movePlayerDown() {m_currentLevel->getPlayer()->moveDown();}

void Game::advance(bool &winLevel, bool &winGame, bool &loose)
{
    m_currentLevel->advance();
    winLevel = m_currentLevel->isWin();

    if (winLevel && (m_currentPos == m_levels.count() -1)) winGame = true;
    else winGame = false;

    if (winLevel) nextLevel();

    if (m_currentLevel->getPlayer()->isDead()) loose = true;
}

void Game::draw(QPainter *painter)
{
    getCurrentMap()->draw(painter);
    getCurrentPlayer()->draw(painter);
    foreach (Monster *monster, getCurrentLevel()->getMonsters())
        if(! monster->isDead()) monster->draw(painter);
    foreach (Rock *rock, getCurrentLevel()->getRocks())
        if(! rock->isDead()) rock->draw(painter);
}
